#version 430 core
precision highp float;
precision highp uimage2D;

layout (binding = 0, offset = 0) uniform atomic_uint fill_counter;
layout (binding = 0, r32ui) coherent uniform uimage2D head_pointer;

// Shader storage buffer containing appended fragments
struct list_item
{
    vec4        color;
    float       depth;
    int         facing;
    uint        next;
};

layout (binding = 0, std430) buffer list_item_block
{
    list_item item[];
};

in vec4 color;
uniform float indexInterval;

void main(void)
{
    ivec2 P = ivec2(gl_FragCoord.xy);
    uint index = atomicCounterIncrement(fill_counter);
    uint old_head = imageAtomicExchange(head_pointer, P, index);

    item[index].color = color * (1.0f / indexInterval);
    item[index].depth = gl_FragCoord.z * (1.0f / indexInterval);
    item[index].facing = gl_FrontFacing ? 1 : 0;
    item[index].next = old_head;
}
